import 'package:fish_redux/fish_redux.dart';
import 'action.dart';
import 'state.dart';
import '../../../app_files.dart';
import '../model/account_model.dart';
import 'dart:convert';

Effect<UserAccountViewState> buildEffect() {
  return combineEffects(<Object, Effect<UserAccountViewState>>{
    UserAccountViewAction.onDelete: _onDelete,
    Lifecycle.initState: _initState,
    Lifecycle.dispose: _dispose,
  });
}

void _initState(Action action, Context<UserAccountViewState> ctx) async {
  List res = await _sendRequest(ctx);
  if (res != null) {
    if (res.isEmpty) {
      CommonUtils.showErrorInfo(ctx.context, '请先绑定户号');
      CommonUtils.pushReplaceRoute(ctx.context, RoutePath.bindAccountView);
    }
  }
  bus.on('refreshAccountList', (arg) {
    _sendRequest(ctx);
  });

  bus.on('bindSuccess', (arg) {
    _sendRequest(ctx);
  });
}

void _dispose(Action action, Context<UserAccountViewState> ctx) {
  bus.off('refreshAccountList');
  bus.off('bindSuccess');
}

void _onDelete(Action action, Context<UserAccountViewState> ctx) async {
  GasAccountModel item = action.payload;
  ctx.dispatch(UserAccountViewActionCreator.onRefreshData(item));
  CommonNetWork.delBlind(item.itemId, item.gasNo);
//  CommonNetWork.delMessage(item.msgId);
}

_sendRequest(Context<UserAccountViewState> ctx) async {
  if (ctx.state.type == 4) {
    ResultData configRes = await CommonNetWork.getSystemConfig();
    if (configRes.success) {
      List data = configRes.data;
      Map config = data.first;
      List value = config['ConfigValue'];
      value.forEach((element) {
        if (element['Key'] == 'NBCtrlCode') {
          String elementValue = element['Value'];
          ctx.state.nbCtrlCodes = json.decode(elementValue);
        }
        if (element['Key'] == 'jxb') {
          String elementValue = element['Value'];
          ctx.state.machineCtrlCodes = json.decode(elementValue);
        }
      });
    }
    ResultData res = await CommonNetWork.getGasNoBlindList();
    if (res.success) {
      List list = res.data ?? [];
      ctx.state.items = [];
      list.forEach((element) {
        GasAccountModel accountModel = GasAccountModel.fromJson(element);
        if (ctx.state.nbCtrlCodes.contains(accountModel.ctrlCode) ||
            ctx.state.machineCtrlCodes.contains(accountModel.ctrlCode))
          ctx.state.items.add(accountModel);
      });
      ctx.dispatch(UserAccountViewActionCreator.onAction());
      return ctx.state.items;
    } else {
      await CommonUtils.showAlertView(ctx.context, res.desc);
      Navigator.pop(ctx.context);
    }
  } else {
    ResultData res = await CommonNetWork.getGasNoBlindList();
    if (res.success) {
      List list = res.data ?? [];
      ctx.state.items = list
          .map<GasAccountModel>((e) => GasAccountModel.fromJson(e))
          .toList();
      ctx.dispatch(UserAccountViewActionCreator.onAction());
      return ctx.state.items;
    } else {
      await CommonUtils.showAlertView(ctx.context, res.desc);
      Navigator.pop(ctx.context);
    }
  }
}
